<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="140px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="流水单号">
              <el-input v-model="form.orderSn"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="回款单号">
              <el-input v-model="form.repaymentSn"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态">
              <el-select v-model="form.isRemove" clearable placeholder="请选择">
                <el-option
                  v-for="item in statusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="医院名称">
              <template>
                <el-select v-model="form.hospitalId" clearable filterable placeholder="请选择">
                  <el-option
                    v-for="item in hospitalOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </template>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="加入时间">
              <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="form.dates"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <!-- <el-form-item> -->
            <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div class="listDiv">
      <!-- table列表 -->

      <el-table :data="tableData" class="tableData" style="width: 100%">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="hospitalName" label="医院"></el-table-column>
        <el-table-column prop="orderSn" label="流水单号"></el-table-column>
        <el-table-column prop="repaymentSn" label="回款单号"></el-table-column>
        <el-table-column prop="userName" label="客户姓名"></el-table-column>
        <el-table-column prop="userPhone" label="客户手机号">
          <template slot-scope="scope">
            <span>{{scope.row.userPhone | phonereg}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="totalPrice" label="消费金额"></el-table-column>
        <el-table-column prop="paymentId" label="付款方式">
          <template slot-scope="scope">
            <span>{{ scope.row.paymentId | paytype}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="创建时间"></el-table-column>
        <el-table-column prop="surplusReceivableAmount" label="剩余回款金额"></el-table-column>
        <el-table-column prop="status" label="回款状态">
          <template slot-scope="scope">
            <span>{{ scope.row.status | RS}}</span>
          </template>
        </el-table-column>

        <el-table-column prop="exceedDays" label="逾期天数">
          <template slot-scope="scope">
            <span>{{ scope.row.isExceed === '0' ? '': scope.row.exceedDays+'天' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="isRemove" label="状态">
          <template slot-scope="scope">
            <span>{{ scope.row.isRemove == 0 ? '未移除':'已移除' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="加入备注"></el-table-column>

        <el-table-column prop="graceTime" label="加入时间 "></el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="100">
          <template slot-scope="scope">
            <span v-if="permissionJson.indexOf('AdminRepaymentProvider.removeGrace') !=-1">
              <el-button
                type="text"
                size="small"
                v-if="scope.row.isRemove == 0"
                @click="deletes(scope.row)"
              >移除</el-button>
            </span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listForm.pageNum"
            :page-sizes="page_sizes"
            :page-size="listForm.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

 <script>
import { hospitalList } from "@/api/financialBack";
import { delayList, delayRemove, expor } from "@/api/refundedDelay";

export default {
  name: "refundedDelay",
  data() {
    return {
      permissionJson: JSON.parse(localStorage.getItem("permissionJson")),
      tableLoading: true,
      officeOption: [],
      statusOptions: [
        {
          value: "",
          label: "全部"
        },
        {
          value: 0,
          label: "未移除"
        },
        {
          value: 1,
          label: "已移除"
        }
      ],
      hospitalOptions: [],
      form: {
        orderSn: "",
        repaymentSn: "",
        hospitalId: null,
        isRemove: null,
        dates: "",
        beginGraceTime: null,
        endGraceTime: null,
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60,
      listForm: {
        pageNum: 1,
        pageSize: 10
      }
    };
  },
  filters: {
    cardType: function(e) {
      return window.helper.cardType(e);
    },
    RS: function(e) {
      switch (e) {
        case "1":
          return "已回款";
        case "2":
          return "未回款";
        case "3":
          return "部分回款";
        case "4":
          return "未抵扣";
        case "5":
          return "已抵扣";
        case "6":
          return "抵扣中";
        case "7":
          return "已付款";
        default:
          return "暂无";
      }
    },
    paytype: function(val) {
      switch (val) {
        case "0":
          return "旧数据默认值";
        case "1":
          return "自费";
        case "2":
          return "分期";
        default:
          return "暂无";
      }
    }
  },
  created() {
    this.getHospital(this.form);
  },
  mounted() {
    this.getList(this.form);
  },
  methods: {
    //按钮权限
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    //成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success"
      });
    },
    //失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    //医院名称
    getHospital() {
      hospitalList().then(res => {
        // console.log(res.data.data)
        if (res.data.data) {
          this.hospitalOptions = res.data.data.map(item => {
            return { value: item.id, label: item.name };
          });
        }
      });
    },
    //查询
    searchFn() {
      this.form.pageNum = 1;
      if (!this.form.hospitalId) {
        this.form.hospitalId = null;
      }
      if (this.form.isRemove === "") {
        this.form.isRemove = null;
      }
      if (this.form.dates) {
        this.form.beginGraceTime = this.form.dates[0];
        this.form.endGraceTime = this.form.dates[1];
      } else {
        this.form.beginGraceTime = null;
        this.form.endGraceTime = null;
      }
      // console.log(this.form)
      this.getList(this.form);
    },
    // 路由跳转
    toRoutes(e, userId) {
      let route = { name: e, query: { userId: userId } };
      this.$router.push(route);
    },
    //列表数据获取
    getList(e) {
      this.tableLoading = true;
      let datas = e;
      delayList(datas)
        .then(res => {
          //  console.log(res)
          if (!res.data.code) {
            // console.log(res)
            this.tableData = res.data.data.list;
            this.page_totals = res.data.data.count;
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    //删除宽限回款单
    deletes(val) {
      this.$prompt("是否确定将此回款单移除宽限回款列表？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPlaceholder: "备注"
      })
        .then(({ value }) => {
          delayRemove({
            repaymentId: val.repaymentId,
            isRemove: "1",
            remark: value,
            isGrace: "0"
          }).then(res => {
            if (res.data.code == 0) {
              this.getList(this.form);
              this.$message({
                type: "success",
                message: "移除成功 "
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消操作"
          });
        });
    },
    //改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    //选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val;
      this.getList(this.form);
    }
  }
};
</script>

<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
/* 表单样式修改 */
.el-range-editor--medium.el-input__inner {
  width: 236px;
}
</style>